package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.OrderInfoEntity;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;

import butterknife.Bind;
import butterknife.ButterKnife;
/*
* 订单编辑 确认界面
* */
public class OrderInfoEditorActivity extends BaseActivity {


    @Bind(R.id.tv_order_number)
    TextView mTvOrderNumber;
    @Bind(R.id.tv_shangpin_name_order_eitor)
    TextView mTvShangpinNameOrderEitor;
    @Bind(R.id.rl_shangpin_name)
    RelativeLayout mRlShangpinName;
    @Bind(R.id.tv_guige_order_eitor)
    TextView mTvGuigeOrderEitor;
    @Bind(R.id.tv_take_order_time_order_eitor)
    TextView mTvTakeOrderTimeOrderEitor;
    @Bind(R.id.tv_majia_tel_order_eitor)
    TextView mTvMajiaTelOrderEitor;
    @Bind(R.id.edit_total_order_editor)
    EditText mTvTotalOrderEditor;
    @Bind(R.id.edit_size_order_editor)
    EditText mEditSizeOrderEditor;

    @Bind(R.id.bt_orderInfo_editor_submit)
    TextView mBtOrderInfoEditorSubmit;

    @Bind(R.id.sv_activity_order_info_editor_view)
    ScrollView sv_activity_order_info_editor_view;
    //======================================
    String order_id;
    OrderInfoEntity infoEntity = new OrderInfoEntity();
    Gson gson = new Gson();
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.arg2!=2){
                if (msg.arg1 == NetConfig.TaskId.receive_order) {
                    finish();
                } else if (msg.arg1 == NetConfig.TaskId.order_info) {
                    infoEntity = gson.fromJson(msg.obj.toString(), OrderInfoEntity.class);
                    AppearData();
                }
            }

            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_info_editor);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            rl_common_title.setPadding(0, dip2px, 0, 0);
        }
        initData();
    }

    private void initData() {
        tvTitle.setText("订单详情编辑");
        order_id = getIntent().getStringExtra("order_id");
        NetWorking(order_id);
        addViewClicklistener(tvBack, mBtOrderInfoEditorSubmit);

        if (Build.VERSION.SDK_INT >= 19) {
            final View decorView = getWindow().getDecorView();
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect rect = new Rect();
                    decorView.getWindowVisibleDisplayFrame(rect);
                    int screenHeight = decorView.getRootView().getHeight();
                    int height = screenHeight - rect.bottom;
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) sv_activity_order_info_editor_view.getLayoutParams();
                    layoutParams.setMargins(0, 0, 0, height);
                    sv_activity_order_info_editor_view.requestLayout();
                }
            });
        }
    }

    private void NetWorking(String order_id) {
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this, NetConfig.SERVER + NetConfig.TYPE_ORDER,
                NetConfig.order_info, mHandler,
                "jsonObject", NetConfig.TaskId.order_info, new Object[]{order_id, new UserImpl(this).getUser().token});
        jsonRPCAsyncTask.execute(new Void[]{});
    }

    double num;
    public void AppearData() {
        mTvOrderNumber.setText("订单号：" + infoEntity.getOrder().getOrder_sn());
        mTvOrderNumber.setTextColor(getResources().getColor(R.color.orange));
        mTvShangpinNameOrderEitor.setText(infoEntity.getOrder().getTitle());
        mTvGuigeOrderEitor.setText(infoEntity.getOrder().getSize());
        mTvTakeOrderTimeOrderEitor.setText(infoEntity.getOrder().getCreate_time());
        mTvMajiaTelOrderEitor.setText(infoEntity.getOrder().getUser_name());
        mTvTotalOrderEditor.setText(infoEntity.getOrder().getPay_fee());
        mEditSizeOrderEditor.setText(infoEntity.getOrder().getGoods_number());

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.imgbtn_commonTitle_back:
                finish();
                break;
            case R.id.bt_orderInfo_editor_submit:
                if(order_id!=null){
                    num=Double.valueOf(mEditSizeOrderEditor.getText().toString().trim());
                    JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(OrderInfoEditorActivity.this, NetConfig.SERVER + NetConfig.TYPE_ORDER,
                            NetConfig.receive_order, mHandler,
                            "jsonObject", NetConfig.TaskId.receive_order, new Object[]{new UserImpl(this).getUser().token,
                            order_id, 1, num,mTvTotalOrderEditor.getText().toString()});
                    jsonRPCAsyncTask.execute(new Void[]{});
                }
                break;
            default:
                break;
        }
    }

}
